package com.app.newiptv.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import com.app.newiptv.bean.LatelyBean;
import com.app.newiptv.bean.allchannels.MainChanel;
import com.app.newiptv.bean.allchannels.Program;
import com.app.newiptv.bean.details.PlayDetailsBean;
import com.app.newiptv.bean.greendao.ProgramDB;
import com.app.newiptv.bean.greendao.SitcomListGen;
import com.app.newiptv.utils.greendao.DbService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Shinelon on 2017/10/29.
 */

public class CacheDataPresenter {
    private List<LatelyBean> detailsBeanList;
    private Gson gson = new Gson();
    private static Context mContext;
    private static SharedPreferences sp;
    private static CacheDataPresenter presenter;

    public List<MainChanel> listContent;

    private Map<String,Program> epgCache=new HashMap<>();



    public static CacheDataPresenter getPresenter(Context content) {
        mContext = content;
        sp = mContext.getSharedPreferences("CACHE_RECORD", Context.MODE_PRIVATE);
        if (presenter == null) {
            presenter = new CacheDataPresenter();
        }
        return presenter;
    }

    public Map<String, Program> getEpgCache() {
        return epgCache;
    }

    /**
     * 缓存播放记录默认20条
     *
     * @param detailsBean
     */
    public void cacheRecord(PlayDetailsBean detailsBean, String cacheName, int countNumber) {
        String usedCache = sp.getString(cacheName, "");
        int id = detailsBean.getId();
        int index = 100;
        if ("".equals(usedCache)) {
            detailsBeanList = new ArrayList<>();
        } else {
            detailsBeanList = gson.fromJson(usedCache, new TypeToken<List<LatelyBean>>() {
            }.getType());
            int count = detailsBeanList.size();
            if (count >= 20) {
                detailsBeanList = detailsBeanList.subList(count - countNumber, detailsBeanList.size());
            }

            for (int i = 0; i < detailsBeanList.size(); i++) {
                if (id == detailsBeanList.get(i).getId()) {
                    index = i;
                }
            }
            if (index < 100) {
                detailsBeanList.remove(index);
            }
        }
        LatelyBean latelyBean = new LatelyBean();
        latelyBean.setId(detailsBean.getId());
        latelyBean.setName(detailsBean.getName());
        latelyBean.setCopyright(detailsBean.getCopyright());
        latelyBean.setDirector(detailsBean.getDirector());
        latelyBean.setInfo(detailsBean.getInfo());
        latelyBean.setPublish_at(detailsBean.getPublish_at());
        latelyBean.setSort(detailsBean.getSort());
        latelyBean.setStaring(detailsBean.getStaring());
        latelyBean.setTotal_episode(detailsBean.getTotal_episode());
        latelyBean.setUrl(detailsBean.getUrl());
        latelyBean.setImage(detailsBean.getImage());
        latelyBean.setType(detailsBean.getType());
        String time = SystemTimeDate.stampToSystemDate(System.currentTimeMillis());
        boolean isTime = true;
        for (int i = 0; i < detailsBeanList.size(); i++) {
            LatelyBean lately = detailsBeanList.get(i);
            if (time != null && time.equals(lately.getLateTime())) {
                isTime = false;
            }
        }
        if (isTime) {
            latelyBean.setLateTime(time);
        } else {
            latelyBean.setLateTime("");
        }
        detailsBeanList.add(latelyBean);

        String recordJson = gson.toJson(detailsBeanList);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(cacheName, recordJson);
        editor.apply();
    }


    public static void cacheSitcom(int id, int index) {
        //index statr from 0, name start from 1
        index=index+1;
        SitcomListGen sitcomListGen = null;
        List<SitcomListGen> sitcomListGens = DbService.getInstance().loadAllNote();
        if (sitcomListGens != null) {
            for (int i = 0; i < sitcomListGens.size(); i++) {
                if (id == sitcomListGens.get(i).getSitcomId()) {
                    sitcomListGen = sitcomListGens.get(i);
                }
            }
            if (sitcomListGen != null) {
                sitcomListGen.setIndexSitcom(sitcomListGen.getIndexSitcom() + "," + index);
                DbService.getInstance().saveNote(sitcomListGen);
            } else {
                sitcomListGen = new SitcomListGen(null, id, index + "");
                DbService.getInstance().saveNote(sitcomListGen);
            }
        }
    }

    public String[] getSitcomIndex(int id) {
        SitcomListGen sitcomListGen = DbService.getInstance().loadData(id);
        if (sitcomListGen != null) {
            String sitcomList = sitcomListGen.getIndexSitcom();
            if (sitcomList != null) {
                String[] sitcomArr = sitcomList.split(",");
                return sitcomArr;
            }
        }
        return null;
    }

    public void saveLiveProgram(Program program) {
        if (program != null) {
            String programStr = gson.toJson(program);
            ProgramDB programDB = new ProgramDB();
            programDB.setId((long) program.getId());
            programDB.setProgramContent(programStr);
            Long count = DbService.getInstance().saveProgramNote(programDB);
            if (count > 0) {
                Toast.makeText(mContext, "收藏成功！", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(mContext, "收藏失败！", Toast.LENGTH_SHORT).show();
            }
            List<ProgramDB> programDBList = DbService.getInstance().loadProgramAllNote();
            for (ProgramDB programs : programDBList) {
                Log.d("tag", "--------programs--------" + programs.toString());
            }
        }
    }

    public List<Program> loadProgramAll() {
        List<ProgramDB> programDBList = DbService.getInstance().loadProgramAllNote();
        List<Program> programList = new ArrayList<>();
        for (ProgramDB programDB : programDBList) {
            if (programDB != null) {
                String programStr = programDB.getProgramContent();
                if (programStr != null && !programStr.isEmpty()) {
                    Program program = gson.fromJson(programStr, Program.class);
                    programList.add(program);
                }
            }

        }
        return programList;
    }

    public List<MainChanel> getListContent() {
        return listContent;
    }

    public void setListContent(List<MainChanel> listContent) {
        this.listContent = listContent;
    }
}
